package com.budwk.app.sqfw.controllers.admin;



import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.dto.RallyExcelDTO;
import com.budwk.app.sqfw.dto.SqfwRallyDTO;


import com.budwk.app.sqfw.dto.SqfwRallyUpdateDTO;
import com.budwk.app.sqfw.models.Sqfw_cenSusPaper;
import com.budwk.app.sqfw.models.Sqfw_rally;
import com.budwk.app.sqfw.services.SqfwCenSusPaperService;
import com.budwk.app.sqfw.services.SqfwRallyService;

import com.budwk.app.sqfwV2.dto.RallyPdfSaveDTO;
import com.budwk.app.sqfwV2.dto.TemplateFileDTO;
import com.budwk.app.sqfwV2.dto.vo.SummaryQueryReqVo;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.starter.common.exception.BaseException;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@IocBean
@At("/platform/sqfw/sqfwRally")
@ApiDefinition(tag = "社区活动")
@Slf4j
public class SqfwRallyController {
    @Inject
    private SqfwRallyService sqfwRallyService;

    @Inject
    private SysDictService sysDictService;

    @Inject
    private SqfwCenSusPaperService sqfwCenSusPaperService;

    @At("/createOne")
    @Ok("json")
    @POST
    @ApiOperation(name = "新增社区活动")
    @ApiResponses
    public Result<?> createOne(@RequestBody Sqfw_rally sqfwRally) {
        sqfwRallyService.createOne(sqfwRally);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "获取活动")
    @ApiResponses
    @SaCheckLogin
    public Result<?> getListJson(@RequestBody SqfwRallyUpdateDTO sqfwRally) {
        Sqfw_rally rally =new Sqfw_rally();
        BeanUtil.copyProperties(sqfwRally,rally);
        Pagination getListPage=sqfwRallyService.getListPage(rally);
        List<SqfwRallyUpdateDTO> sqfwRallyUpdateDTOS=new ArrayList<>();
        getListPage.getList(Sqfw_rally.class).stream().forEach(s->{
            SqfwRallyUpdateDTO sqfwRallyUpdateDTO = new SqfwRallyUpdateDTO();
            BeanUtil.copyProperties(s,sqfwRallyUpdateDTO);
            sqfwRallyUpdateDTOS.add(sqfwRallyUpdateDTO);
        });
        sqfwRallyUpdateDTOS.forEach(d->{
            d.setRallyPdfSaveDTO(JSONUtil.toBean(d.getRallyContentJson(), RallyPdfSaveDTO.class));
            d.setRallyContentJson(null);
        });
        getListPage.setList(sqfwRallyUpdateDTOS);
        return Result.success(getListPage);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增社区活动")
    @ApiResponses
    public Result<?> createOneJson(@RequestBody SqfwRallyUpdateDTO sqfwRally) {
        return Result.data( sqfwRallyService.createOneJson(sqfwRally));
    }

    @At("/updateOne")
    @Ok("json")
    @POST
    @ApiOperation(name = "修改社区活动")
    @ApiResponses
    public Result<?> updateOne(@Param("..") SqfwRallyUpdateDTO sqfwRally) {
        return Result.success(sqfwRallyService.updateOne(sqfwRally));
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "草稿箱发布")
    @ApiResponses
    @SaCheckLogin
    public Result<?> updateDraft(@Param("..") String  id) {
        Sqfw_rally rally = sqfwRallyService.fetch(id);
        if(ObjectUtil.isEmpty(rally)){
            throw  new BaseException("无法找到对象Sqfw_rally");
        }
        if(!rally.getRallyReleaseUserId().equals(SecurityUtil.getUserId())){
            throw  new BaseException("发布人不一样");
        }
        sqfwRallyService.update(Chain.make("draft",2),
                Cnd.where("id", "=",id));
        return Result.success();
    }



    @At("/getOne")
    @Ok("json:full")
    @POST
    @ApiOperation(name = "获取社区活动")
    @ApiResponses
    public Result<?> getOne(@Param("..") SqfwRallyDTO sqfwRally) {
        return Result.success( sqfwRallyService.getOne(sqfwRally));
    }
    @At
    @Ok("json:full")
    @POST
    @ApiOperation(name = "获取社区活动")
    @ApiResponses
    public Result<?> getOneJson(@Param("..") SqfwRallyDTO sqfwRally) {
        return Result.success( sqfwRallyService.getOneJson(sqfwRally));
    }
    @At("/getSpecial")
    @Ok("json")
    @GET
    @ApiOperation(name = "获取特殊人群")
    @ApiResponses
    public Result<?> getSpecial() {
        return Result.success( sysDictService.getSubListByCode("special_group"));
    }

    @At("/finishService")
    @Ok("json")
    @POST
    @ApiOperation(name = "完结任务")
    @ApiResponses
    public Result<?> finishService(@Param("..") Sqfw_rally sqfwRally) {
        sqfwRallyService.finishService(sqfwRally);
        return Result.success();
    }

    @At("/delRally")
    @Ok("json")
    @GET
    @ApiOperation(name = "删除任务")
    @ApiResponses
    public Result<?> delRally(@Param("id") String id) {
        sqfwRallyService.delRally(id);
        return Result.success();
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "删除任务")
    @ApiResponses
    public Result<?> delRallyJson(@Param("id") String id) {
        sqfwRallyService.delRallyJson(id);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "删除任务")
    @ApiResponses
    public Result<?> delRallyJson(@Param("ids")List<String>  ids) {
        sqfwRallyService.delRallyJson(ids);
        return Result.success();
    }

    @At("/getConsumer")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取小程序用户")
    @ApiResponses
    public Result<?>  getConsumer(@Param("..")  SqfwRallyDTO sqfwRally) {
        Sqfw_cenSusPaper r= sqfwCenSusPaperService.fetch(Cnd.where("phone","=",sqfwRally.getPhone()));
        if(ObjectUtil.isNotEmpty(r)){
            return Result.success(r);
        }
        return Result.success(200500,"您的信息缺失无法报名，请您联系居委会补齐信息！");
    }

    @At("/generateRallyExcel")
    @Ok("json")
    @POST
    @ApiOperation(name = "根据条件下载excel")
    @SLog(tag = "根据条件下载excel", value = "${args[0]}")
    public Result<?> generateRallyExcel(@Param("id") String id) {
        RallyExcelDTO rallyExcelDTO= sqfwRallyService.generateRallyExcel(id);
        return Result.data( rallyExcelDTO);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据条件下载excel")
    @SLog(tag = "根据条件下载excel", value = "${args[0]}")
    public Result<?> generateRallyExcelV2(@Param("id") String id) {
        RallyExcelDTO rallyExcelDTO= sqfwRallyService.generateRallyExcelV2(id);
        return Result.data( rallyExcelDTO);
    }

    @At("/getPeopleById")
    @Ok("json")
    @GET
    @ApiOperation(name = "获取报名人员")
    @SLog(tag = "获取报名人员")
    public Result<?> getPeopleById(@Param("rallyId") String rallyId) {
        return Result.data(sqfwRallyService.getPeople(rallyId));
    }

    @At("/arriveMark")
    @Ok("json")
    @GET
    @ApiOperation(name = "报名人员签到")
    @SLog(tag = "报名人员签到")
    public Result<?>  arriveMark(@Param("id") String id) {
        return Result.success( sqfwRallyService.arriveMark(id));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "报名人员签到")
    public Result<?>  arriveMarkBatch(@Param("ids") List<String>  ids) {
        sqfwRallyService.arriveMarkBatch(ids);
        return Result.success();
    }
    @At("/manyArriveMark")
    @Ok("json")
    @GET
    @ApiOperation(name = "一键签到")
    @SLog(tag = "一键签到")
    public Result<?>  manyArriveMark(@Param("rallyId")  String rallyId) {
        sqfwRallyService.manyArriveMark(rallyId);
        return Result.success();
    }


    @At
    @Ok("json")
    @POST
    public Result<?> changeTemplate( ) {
        List<TemplateFileDTO> teplateFileDTOS= sqfwRallyService.changeTemplate(null);
        return Result.success(teplateFileDTOS);
    }


    @At
    @Ok("json")
    @POST
    public Result<?> changeTemplateByDate(@Param("..") SummaryQueryReqVo reqVo ) {
        return Result.data(sqfwRallyService.generateDetailFile(reqVo));
    }

    @At
    @Ok("json")
    @POST
    public Result<?> generateByCode(@Param(value = "code") int code,@Param(value = "startTime")String startTime,@Param(value = "endTime")String endTime) {
        TemplateFileDTO teplateFileDTO=sqfwRallyService.generateByCode(code,startTime,endTime, null);
        return Result.success(teplateFileDTO);
    }

    @At
    @GET
    @Ok("json")
    public Result<?> importExcel() {
        List<Sqfw_rally> dd=  sqfwRallyService.query(Cnd.where(Sqfw_rally::getCommunity,"=","龙吉社区"));
        dd.forEach(s->{
            RallyPdfSaveDTO rallyPdfSaveDTO=new RallyPdfSaveDTO();
            rallyPdfSaveDTO=JSONUtil.toBean(s.getRallyContentJson(),RallyPdfSaveDTO.class);
            rallyPdfSaveDTO.setEventRegistrationClosingTime(s.getConditions() );
            s.setRallyContentJson(JSONUtil.toJsonStr(rallyPdfSaveDTO));
            s.setConditions(" ");
            sqfwRallyService.update(s);
        });
        return Result.success();

    }
}